<?php
    $pag = $_GET["n"];
    $nperpag = 5;
    session_start();
    require_once 'php/database.php';

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }

    
?>

<?php
                    $user = getUserNamePage();
                    $hasil1 = database::query("SELECT IDThread FROM messagetarget WHERE Username = '$user'");
                    if(mysql_num_rows($hasil1)>0){
                        echo '<div id="daerahjudulpesan">';
                        echo '<p class="judul">Pilih Judul</p>';
                        $indeksArray = 0;
                        while ($baris1 = mysql_fetch_row($hasil1)){
                            $idthread = $baris1[0];

                            $hasil2 = database::query("SELECT JudulThread FROM thread WHERE IDThread = '$idthread'");
                            if(mysql_num_rows($hasil2)>0){
                                $baris2 = mysql_fetch_row($hasil2);
                                $judul = $baris2[0];

                            }

                            $hasil4 = database::query("SELECT * FROM message WHERE IDThread = '$idthread' ORDER BY TanggalKirim DESC");
                            if(mysql_num_rows($hasil4)>0){
                                $baris4 = mysql_fetch_row($hasil4);
                                $waktu = $baris4[3];
                                $sipengirim = $baris4[0];
                            }

                            $idArray[$indeksArray] = $idthread;
                            $judulArray[$indeksArray] = $judul;
                            $waktuArray[$indeksArray] = $waktu;
                            $sipengirimArray[$indeksArray] = $sipengirim;

                            ++$indeksArray;

                            /*echo '<div class="judulpesan">';
                                echo '<a href="messagedetail.php?un='.$user.'&id='.$idthread.'">'.$judul.'<br/></a>';
                                //echo '<a href="javascript:void()" onclick="loadXMLMessageDoc1(\''.$user.'\',\''.$idthread.'\')">'.$judul.'<br/></a>';
                                echo '<div>';
                                    echo '<label>';
                                        echo ''.$waktu.' by ';
                                    echo '</label>&nbsp';
                                    echo '<label>';
                                        echo ''.$sipengirim.'';
                                    echo '<br/></label>';
                                echo '</div>';
                            echo '</div>';*/
                        }

                        
                        if (($indeksArray % $nperpag) ==  0){
                            $ntemp = (($indeksArray - ($indeksArray % $nperpag)) / $nperpag);
                        }
                        else{
                            $ntemp = (($indeksArray - ($indeksArray % $nperpag)) / $nperpag) + 1;
                        }
                        if ($ntemp > 0){
                            $npage = $ntemp - 1;
                        }


                        for ($j = 0; $j < $nperpag; ++$j){
                            echo '<div class="judulpesan">';
                                $j2 = $pag * $nperpag + $j;
                                if ($j2 < $indeksArray){
                                    //echo '<a href="messagedetail.php?un='.$user.'&id='.$idArray[$j2].'">'.$judulArray[$j2].'<br/></a>';
                                    echo '<a href="javascript:void()" onclick="goToMessageDetail(\''.$user.'\',\''.$idArray[$j2].'\')">'.$judulArray[$j2].'<br/></a>';
                                    echo '<div>';
                                        echo '<label>';
                                            echo ''.$waktuArray[$j2].' by ';
                                        echo '</label>&nbsp';
                                        echo '<label>';
                                            echo ''.$sipengirimArray[$j2].'';
                                        echo '<br/></label>';
                                    echo '</div>';
                                }
                            echo '</div>';
                        }

                        echo '</div>';
                        echo '<div align="center">';
                            echo '<input type="button" id="nextpesan" name="nextpesan" value="prev" onclick="prevpes('.$pag.',\''.$user.'\','.$npage.')">';

                            echo '</input>';
                            echo '<input type="button" id="prevpesan" name="prevpesan" value="next" onclick="nextpes('.$pag.',\''.$user.'\','.$npage.')">';

                            echo '</input>';
                        echo '</div>';
                    }

                    else{
                        echo '<div id="daerahjudulpesan">';
                        echo '<p class="judul">Belum Ada Pesan</p>';
                        echo '</div>';
                    }

?>